package com.example.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @program: sms
 * @description:
 * @author: zhanghz001
 * @create: 2020-11-04 10:25
 **/
// @WebFilter("/WEB-INF/pages/success.jsp")
//和servlet 一样,也是单例的
public class FilterDemo01 implements Filter {
    private FilterConfig filterConfig;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
        System.out.println("filter init");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request1 = (HttpServletRequest) request;
        StringBuffer url = request1.getRequestURL();
        if (url.toString().contains("favicon.ico")) {
            return;
        }

        System.out.println("FilterDemo01执行拦截");
        Enumeration<String> names = filterConfig.getInitParameterNames();
        while (names.hasMoreElements()) {

            String s = names.nextElement();
            String initParameter = filterConfig.getInitParameter(s);
            System.out.println(s + "  :  " + initParameter);
        }

        System.out.println(filterConfig.getFilterName());
        System.out.println("==============");
        System.out.println(filterConfig.getInitParameter("name"));
        System.out.println(filterConfig.getInitParameter("age"));

        //过滤器链对象,调用下一个过滤器链对象,如果是最后一个过滤器链对象,调用目标资源
        chain.doFilter(request, response);
        System.out.println("执行了对象,看看是啥时候执行");
    }

    @Override
    public void destroy() {
        System.out.println("destroy");
    }
}
